<template>
  <div id="indexDiv" class="indexDiv m-padded-tb-large" style="">

    <!--顶部图片-->
    <div style="margin-top: -30px">
      <!--页面首图-->
      <div class="blog_first_picture">
        <img src="../assets/img/index2.jpg"  alt="首页首图" />
      </div>
    </div>

    <!--页面首图上的文字-->
    <div style="margin-top: -650px">
      <h2 style="font-family: STSong; font-size: 45px; font-weight: 900; color: #ffffff;" align="center" >欢迎来到我的客栈</h2>
      <div class="ui container" align="center" style="margin-top: 20px">
        <div class="ui horizontal link list" align="center">
          <div class="item">
<!--            <div class="ui orange basic label" style="font-size: 15px"></div>-->
          </div>
        </div>
      </div>
    </div>

    <div style="margin-top: 478px; margin-bottom: 60px">

      <el-row :gutter="30">
        <!-- 左边整个博客列表 -->
        <el-col :span="9" :offset="5">
          <div class="m-border" style=" ">
            <!-- 排头 -->
            <div  class="m-leader" style=" border-bottom: 1px solid #bbbbbb; height: 60px;">
              <div style="float: left; margin-left: 10px; height:60px">
                <h2 style="font-weight: 700; margin-top: 14px; color: rgb(0, 181, 173)">博客</h2>
              </div>
              <div style="float: right; margin-right: 10px; height:60px">
                共 <h2 class="m-inline-block m-text-thin" style="margin-top: 14px; color: rgb(244, 113, 28)">{{ total }}</h2> 篇
              </div>
            </div>

            <!-- 博客列表 -->
            <div id="blogListDiv" style="margin-bottom: 50px;">

              <!-- 博文 -->
              <div class="blogItemBottomDiv" v-for="(blog, index) in blogs" :key="blog.id" >
                <div class="blogItemDive" style="margin-bottom: 27px">
                  <el-row>
                    <!-- 文章内容 -->
                    <el-col :span="16">
                      <div style="margin-left: 20px">
                        <h3 style="margin-top: 0px; cursor:pointer;" @click="toBlogInfo(blog.id)">{{ blog.title }}</h3>
                        <p class="m-text">{{ blog.description }}</p>
                        <div>
                          <el-row>
                            <el-col :span="17">
                              <div>
                                <div style="float: left"><el-avatar shape="circle" size="small" :src="blog.avatar" alt="头像"></el-avatar></div>
                                <div style="float: left; margin-top: 1px;"><el-link href="#/aboutMe" :underline="false" style="color: #0758F8; font-weight: 600; margin-left: 7px">{{ blog.nickname }}</el-link></div>
                                <div style="float: left; margin-left: 15px; margin-top: 4px; font-size: 10px; color: #bbbbbb;"><i class="el-icon-date">&nbsp;{{ blog.updateTime | dateFormat('YYYY-MM-DD HH:mm') }}</i></div>
                                <div style="float: left; margin-left: 12px; margin-top: 4px; font-size: 10px; color: #bbbbbb;"><i class="el-icon-view">&nbsp;{{ blog.views }}</i></div>
                              </div>
                            </el-col>

                            <el-col :span="7">
                              <el-button class="m-text-thin m-padded-tiny"
                                         @click="toTypeInfo(blog.typeId)"
                                         plain size="medium"
                                         style="border-color: #07f686; float: right; color: #08BF6A;">
                                {{ blog.typeName }}
                              </el-button>
                            </el-col>
                          </el-row>
                        </div>
                      </div>
                    </el-col>

                    <!-- 图片 -->
                    <el-col :span="8">
                      <div style="margin-right: 15px; margin-top: 10px" align="right">
                        <el-link :underline="false">
                          <img @click="toBlogInfo(blog.id)"
                               :src="blog.firstPicture" alt="博客首图"
                               style="border-radius: 5px" width="90%" height="110vw">
                        </el-link>
                      </div>
                    </el-col>
                  </el-row>
                </div>
              </div>
            </div>

            <!--  分页  -->
            <div class="page_index" style="margin-top: -20px">
              <div>
                <div style="float: left">
                  <el-pagination
                    background
                    prev-text="上一页"
                    next-text="下一页"
                    layout="prev, pager, next, jumper, total"
                    :page-size="size"
                    :current-page="pageNow"
                    :page-sizes="[2,4,6,8,10]"
                    @current-change="findPage"
                    @size-change="findSize"
                    :total="total">
                  </el-pagination>
                </div>
                <div style="clear: both"></div> <!--在 .head div 最后补一个空DIV 清除浮动-->
              </div>
            </div>
          </div>
        </el-col>

        <!-- 右边的top -->
        <el-col :span="5">
          <div>

            <!-- 分类 -->
            <div class="m-border" align="center">
              <!-- 分类标题 -->
              <div style="background-color: #f5f5c1; border-radius:5px ">
                <div style=" border-bottom: 2px solid rgb(0, 181, 173); height: 50px">
                  <div style="float: left; margin-left: 20px; margin-top: 14px; height:60px; color: rgb(97, 98, 98)">
                    <i class="el-icon-s-opportunity">分类</i  >
                  </div>
                  <div style="float: right; margin-right: 20px; margin-top: 12px; height:60px;">
                    <el-link href="#/types" :underline="false" style="font-size: 15px">more<i class="el-icon-d-arrow-right"></i></el-link>
                  </div>
                </div>
              </div>
              <!-- 分类列表 -->
              <div
                   class="m-border"
                   align="center"
                   style="margin-top: 18px; margin-bottom: 30px; width: 80%; border-radius: 5px;" >
                <el-menu style="border-radius: 5px;background-color: rgb(245,245,220);"
                         v-for="(typeBlogCount, index) in typeBlogCountList.slice(0, 6)" :key="typeBlogCount.typeId">
                  <el-menu-item index="2" @click="toTypeInfo(typeBlogCount.typeId, index)">
                    <div class="m-type-item-left"><i>{{ typeBlogCount.typeName }}</i></div>
                    <div class="box m-type-item-right"><span>{{ typeBlogCount.blogCount }}</span></div>
                  </el-menu-item>
                </el-menu>
              </div>
            </div>

            <!-- 标签 -->
            <div class="m-border" align="center" style="margin-top: 40px">
              <!-- 标签标题 -->
              <div style="background-color: #f5f5c1; border-radius:5px ">
                <div style=" border-bottom: 2px solid rgb(0, 181, 173); height: 50px">
                  <div style="float: left; margin-left: 20px; margin-top: 14px; height:60px; color: rgb(97, 98, 98)">
                    <i class="el-icon-s-flag">标签</i  >
                  </div>
                  <div style="float: right; margin-right: 20px; margin-top: 12px; height:60px;">
                    <el-link href="#/tags" :underline="false" style="font-size: 15px">more<i class="el-icon-d-arrow-right"></i></el-link>
                  </div>
                </div>
              </div>
              <!-- 标签列表 -->
              <div class=""
                   align="center"
                   style="margin-top: 18px; margin-bottom: 30px; width: 80%; border-radius: 5px; text-align: center" >
                <a @click="toTagInfo(tagBlogCount.tagId, index)"
                   v-for="(tagBlogCount, index) in tagBlogCountList.slice(0, 22)" :key="tagBlogCount.tagId"
                   class="ui teal basic left pointing label m-margin-tb-tiny m-border">
                  {{ tagBlogCount.tagName }}
                  <div class="detail" >{{ tagBlogCount.blogCount }}</div>
                </a>
              </div>
            </div>

            <!-- 最新推荐 -->
            <div class="m-border" align="center" style="margin-top: 40px">

              <div style="background-color: #f5f5c1; border-radius:5px ">
                <div style=" border-bottom: 2px solid rgb(0, 181, 173); height: 50px; padding-top: 10px">
                  <i class="el-icon-collection-tag" style="font-size: 25px">最新推荐</i  >
                </div>
              </div>

              <div class="ui segments m-margin-top-large" style="width: 80%; text-align: left; margin-bottom: 30px;">
                <div
                  class="ui segment"
                  v-for="(recommendBlog, index) in recommendBlogs.slice(0, 6)" :key="recommendBlogs.id"
                  style="background-color: transparent">
                  <a @click="toBlogInfo(recommendBlog.id)" class="m-black m-text-thin" style="cursor:pointer;">{{ recommendBlog.title }}</a>
                </div>
              </div>

            </div>
            <!-- 扫码关注 -->
            <div class=""style="margin-top: 50px">
              <el-divider content-position="center">
                <div style="background-color: rgb(245, 245, 220); font-size: 25px; font-weight: 500">
                  扫码关注我
                </div>
              </el-divider>
              <div  style="margin-top: 30px" align="center">
                <img class="m-border" src="../assets/img/wechat.jpg" alt="公众号二维码" width="200px">
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
export default {
  name: "Index",
  data() {
    return {
      blogs: [],
      recommendBlogs: [],
      typeBlogCountList: [],
      tagBlogCountList: [],
      blogFirstPicture: "",
      total: 0,
      size: 8,
      pageNow: 1
    };
  },
  methods:{
    findSize(size) {
      // console.log(size);
      this.size = size;
      this.findAllBlogs(this.page,size);
    },
    // 用来处理分页相关方法
    findPage(page) {
      // console.log(page)
      this.page = page;
      this.findAllBlogs(page, this.size);
    },
    // 查询博客列表
    findAllBlogs(page, size) {
      page = page ? page : this.pageNow,
        size = size ? size : this.size;
      this.$http.post("http://localhost:8088/show/blogs/list",{
        pageNum:page,
        pageSize:size,
        published:true,
        orderField:'views',
        direction:'desc'
      }).then(res => {
        this.blogs = res.data.data;
        this.total = res.data.total;
      });
    },
    // 获取所有发布且推荐的博客，并根据时间降序排列
    findPublishedAndRecommendBlogs(){
      this.$http.get("http://localhost:8088/show/blogs/pAndRList").then(res => {
        this.recommendBlogs = res.data.data;
      });
    },
    // 查询分类对应博客数量列表
    getTypeBlogCountList(){
      this.$http.get("http://localhost:8088/show/types/blogCount").then(res => {
        if (res.data.code == 0) { // 成功
          this.typeBlogCountList = res.data.data;
        }
      });
    },
    // 查询标签对应博客数量列表
    getTagBlogCountList(){
      this.$http.get("http://localhost:8088/show/tags/blogCount").then(res => {
        if (res.data.code == 0) { // 成功
          this.tagBlogCountList = res.data.data;
        }
      });
    },
    // 跳转至博客详情页面
    toBlogInfo(id){
      let routeData = this.$router.resolve({ // 路由跳转并传参
        name:'blog',
        query:{
          id:id //博客id
        },
      });
      window.open(routeData.href, '_blank');
    },
    // 跳转至分类页面
    toTypeInfo(id, index){
      this.$router.push({ // 路由跳转并传参
        name:'types',
        params:{
          id: id, //分类id
          index: index
        },
      });
    },
    // 跳转至标签页面
    toTagInfo(id, index){
      this.$router.push({ // 路由跳转并传参
        name:'tags',
        params:{
          id:id, //标签id
          index: index
        },
      });
    },
  },
  created() {
    this.findAllBlogs();// 查询博客列表
    this.findPublishedAndRecommendBlogs() // 获取所有发布且推荐的博客，并根据时间降序排列
    this.getTypeBlogCountList();// 分类对应博客数量列表
    this.getTagBlogCountList();// 标签对应博客数量列表
  }
}
</script>

<style scoped>
#indexDiv{

}

.m-type-item-left{
  float: left;
  color: rgb(0, 181, 173);
  font-size: 20px;
  margin-left: 20px;
}

.m-type-item-right{
  float: right;
  font-size: 18px;
  color: rgb(0, 181, 173);
  margin-right: 20px;
}

.box:before{
  content:'';
  position:absolute;right:0%;
  top:19px;width:0;height:0;
  border-width:10px;
  border-style:solid;
  border-color:transparent;
  margin-bottom:8px;
  border-right-width:38px;
  border-right-color:currentColor;
  color:rgb(0, 181, 173);
}

.el-divider--horizontal {
  margin: 8px 0;
  margin-top: 30px;
  background: 0 0;
  border-top: 1px solid #bbbbbb;
}

.el-divider__text {
  position: absolute;
  background-color: rgb(245, 245, 220);
  padding: 0 20px;
  color: #303133;
}

/* 分页设置 */
.page_index{
  background-color: rgb(242, 242, 242);
  padding-top: 10px;
  padding-bottom: 10px
}

/*还没用*/
.m-test{
  cursor: pointer;
  display: inline-block;
  background-color: #f5f5c1;
}

/* 顶部图片——图片等比例占满外层容器 */
.blog_first_picture{
  width: 100%;
  height: 800px;
}
.blog_first_picture img{
  width: 100%;
  height: 100%;
  object-fit:cover;
  z-index: -1;
}

</style>
